<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container tw-filter">

<!--      <el-input v-model="queryList.name" placeholder="请输入企业名称" class="filter-item tw-filter-input" clearable />-->
<!--      <el-input v-model="queryList.customer_legal_representative" placeholder="请输入法人" class="filter-item tw-filter-input" clearable />-->
<!--      <el-input v-model="queryList.contacts_phone" placeholder="请输入联系手机号" class="filter-item tw-filter-input" clearable />-->
      <el-select v-model="queryList.status" class="filter-item tw-filter-input" placeholder="审核状态" clearable>
        <el-option key="" label="审核状态" value="" />
        <el-option key="1" label="已审核" value="1" />
        <el-option key="0" label="未审核" value="0" />
      </el-select>
      <el-select v-model="queryList.type" class="filter-item tw-filter-input" placeholder="供需类型" clearable>
        <el-option key="" label="供应类型" value="" />
        <el-option key="1" label="需求" value="1" />
        <el-option key="0" label="供应" value="0" />
      </el-select>

      <el-button v-waves class="filter-item tw-filter-button" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <!-- 表格内容 -->
    <el-table
      v-loading="listLoading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border fit highlight-current-row style="width: 100%;"
    >

      <el-table-column label="ID" prop="id" align="center" width="80" />
      <el-table-column label="用户名" prop="user_name" align="left" />
      <el-table-column label="公司名称" prop="company_name" align="left" />
      <el-table-column label="手机号" prop="mobile" align="center" />
      <el-table-column label="内容" prop="content" align="left" />
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.status === 0">未审核</span>
          <span v-else-if="row.status === 1">已审核</span>
          <span v-else>已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="供需类型" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.type === 0">供应</span>
          <span v-else-if="row.type === 1">需求</span>
          <span v-else>已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="{ row }">
          <el-button size="mini" @click="handleDetail(row)">查看</el-button>
          <el-button
            size="mini"
            type="success"
            v-show="row.status === 0"
            @click="handleAudit(row)"
          >
            审核
          </el-button>

          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />

    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="dialogFormVisibleDetail" width="40%">
      <el-form ref="dataFormDetail" :model="detailData" label-position="left" label-width="100px" style="width: 80%; margin-left:50px;">
        <el-form-item label="用户 ID">{{ detailData.user_id }}</el-form-item>
        <el-form-item label="用户名">{{ detailData.user_name }}</el-form-item>
        <el-form-item label="公司名称">{{ detailData.company_name }}</el-form-item>
        <el-form-item label="手机号">{{ detailData.mobile }}</el-form-item>
        <el-form-item label="内容">{{ detailData.content }}</el-form-item>
        <el-form-item label="状态">
          <span v-if="detailData.status === 0">未审核</span>
          <span v-else-if="detailData.status === 1">已审核</span>
          <span v-else>已删除</span>
        </el-form-item>
        <el-form-item label="供需类型">
          <span v-if="detailData.type === 0">供应</span>
          <span v-else>="detailData.type === 1">需求</span>
        </el-form-item>
        <el-form-item label="创建时间">{{ detailData.created_at }}</el-form-item>
        <el-form-item label="更新时间">{{ detailData.updated_at }}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleDetail = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {del} from "@/api/common/common";

export default {
  data() {
    return {
      queryList: {
        name: '',
        customer_legal_representative: '',
        contacts_phone: '',
        status: '',
        type: '',
        curpage: 1,
        page: 10,
      },
      //键字段
      keyName: 'id',
      apiPath: 'operate.supply',
      tableList: [],
      listLoading: false,
      isPage: true,
      listTotal: 0,
      dialogFormVisibleDetail: false,
      detailData: {},
    };
  },
  methods: {
    handleFilter() {
      this.getList();
    },
    getList() {
      this.listLoading = true;
      this.$api.fetchList(this.queryList, this.apiPath).then(response => {
        this.tableList = response.list;
        this.listTotal = response.list_count;
        this.isPage = response.is_page;
        this.listLoading = false;
      });
    },
    handleDetail(row) {
      this.detailData = row;
      this.dialogFormVisibleDetail = true;
    },
    handleAudit(row) {
      this.$confirm('确定审核通过吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // 审核逻辑
        this.$api.edit({"status":1,"id":row.id}, this.apiPath).then(response => {
          this.listLoading = false;
        });
        // 重新获取列表
        this.getList()
        this.$message({type: 'success', message: '审核成功!'});
      });
    },
    handleDelete(row) {
      this.$confirm('确定删除这条记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger',
      }).then(() => {
        // 审核逻辑
        this.$api.del({"id_str":row.id}, this.apiPath).then(response => {
          this.listLoading = false;
        });
        // 重新获取列表
        this.getList()
        this.$message({type: 'success', message: '删除成功!'});
      });
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
</style>
